package zmee.pmct.opplugin;

import kd.bos.entity.ExtendedDataEntity;
import kd.bos.entity.plugin.AbstractOperationServicePlugIn;
import kd.bos.entity.plugin.AddValidatorsEventArgs;
import kd.bos.entity.plugin.PreparePropertysEventArgs;
import kd.bos.entity.validate.AbstractValidator;

import java.util.List;

/**
 * @Author: zyj
 * @Date: 2023/7/3 16:23
 * @Description: 支出合同: 合同编码不允许存在空格
 */
public class OutContractBillNoOpPlugin extends AbstractOperationServicePlugIn {

    @Override
    public void onPreparePropertys(PreparePropertysEventArgs e) {
        super.onPreparePropertys(e);
        List<String> fieldKeys = e.getFieldKeys();
        fieldKeys.add("billno");
    }

    @Override
    public void onAddValidators(AddValidatorsEventArgs e) {
        super.onAddValidators(e);
        e.addValidator(new AbstractValidator() {
            @Override
            public void validate() {
                ExtendedDataEntity[] dataEntities = this.getDataEntities();
                for (ExtendedDataEntity dataEntity : dataEntities) {
                    // 获取单据编码
                    String billno = (String) dataEntity.getValue("billno");
                    // 通过空格分割 这样第一个是空格或者字符串中间出现空格splits数组长度超过2, 但是不能预防尾部出现空格
                    String[] splits = billno.split(" ");
                    // 去除前后两端的空格
                    String trim = billno.trim();
                    if (splits.length > 1 || billno.length() != trim.length()) {
                        addErrorMessage(dataEntity,"合同编码存在空格,不允许保存或提交,请检查！");
                    }
                }
            }
        });
    }
}
